package com.lanhe.weichatTransfer.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lanhe.common.utils.DateUtils;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.common.utils.wechat.WxTiXianUtil;
import com.lanhe.member.domain.LhMemberWithdrawalRec;
import com.lanhe.member.mapper.LhMemberWithdrawalRecMapper;
import com.lanhe.order.domain.LhOrdersWithdrawal;
import com.lanhe.order.mapper.LhOrdersWithdrawalMapper;
import com.lanhe.weichatTransfer.domain.LhOrdersWeichatTransfer;
import com.lanhe.weichatTransfer.mapper.LhOrdersWeichatTransferMapper;
import com.lanhe.weichatTransfer.service.ILhOrdersWeichatTransferService;
import com.lanhe.weichatTransferRec.domain.LhOrdersWeichatTransferRec;
import com.lanhe.weichatTransferRec.mapper.LhOrdersWeichatTransferRecMapper;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;
import com.wechat.pay.java.service.transferbatch.model.GetTransferBatchByOutNoRequest;
import com.wechat.pay.java.service.transferbatch.model.TransferBatchEntity;
import com.wechat.pay.java.service.transferbatch.model.TransferBatchGet;
import com.wechat.pay.java.service.transferbatch.model.TransferDetailCompact;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 微信批量提现账单记录Service业务层处理
 * 
 * @author tianliang
 * @date 2024-02-29
 */
@Service
public class LhOrdersWeichatTransferServiceImpl implements ILhOrdersWeichatTransferService
{
    @Autowired
    private LhOrdersWeichatTransferMapper lhOrdersWeichatTransferMapper;

    @Autowired
    private LhOrdersWeichatTransferRecMapper lhOrdersWeichatTransferRecMapper;

    @Autowired
    private LhMemberWithdrawalRecMapper lhMemberWithdrawalRecMapper;

    @Autowired
    private LhOrdersWithdrawalMapper lhOrdersWithdrawalMapper;

    @Autowired
    private WxTiXianUtil wxTiXianUtil;

    /**
     * 查询微信批量提现账单记录
     * 
     * @param batchTransferId 微信批量提现账单记录主键
     * @return 微信批量提现账单记录
     */
    @Override
    public LhOrdersWeichatTransfer selectLhOrdersWeichatTransferByBatchTransferId(Long batchTransferId)
    {
        return lhOrdersWeichatTransferMapper.selectLhOrdersWeichatTransferByBatchTransferId(batchTransferId);
    }

    /**
     * 查询微信批量提现账单记录列表
     * 
     * @param lhOrdersWeichatTransfer 微信批量提现账单记录
     * @return 微信批量提现账单记录
     */
    @Override
    public List<LhOrdersWeichatTransfer> selectLhOrdersWeichatTransferList(LhOrdersWeichatTransfer lhOrdersWeichatTransfer)
    {
        return lhOrdersWeichatTransferMapper.selectLhOrdersWeichatTransferList(lhOrdersWeichatTransfer);
    }

    /**
     * 新增微信批量提现账单记录
     * 
     * @param lhOrdersWeichatTransfer 微信批量提现账单记录
     * @return 结果
     */
    @Override
    public int insertLhOrdersWeichatTransfer(LhOrdersWeichatTransfer lhOrdersWeichatTransfer)
    {
        lhOrdersWeichatTransfer.setCreateTime(DateUtils.getNowDate());
        return lhOrdersWeichatTransferMapper.insertLhOrdersWeichatTransfer(lhOrdersWeichatTransfer);
    }

    /**
     * 修改微信批量提现账单记录
     * 
     * @param lhOrdersWeichatTransfer 微信批量提现账单记录
     * @return 结果
     */
    @Override
    public int updateLhOrdersWeichatTransfer(LhOrdersWeichatTransfer lhOrdersWeichatTransfer)
    {
        lhOrdersWeichatTransfer.setUpdateTime(DateUtils.getNowDate());
        return lhOrdersWeichatTransferMapper.updateLhOrdersWeichatTransfer(lhOrdersWeichatTransfer);
    }

    /**
     * 批量删除微信批量提现账单记录
     * 
     * @param batchTransferIds 需要删除的微信批量提现账单记录主键
     * @return 结果
     */
    @Override
    public int deleteLhOrdersWeichatTransferByBatchTransferIds(Long[] batchTransferIds)
    {
        return lhOrdersWeichatTransferMapper.deleteLhOrdersWeichatTransferByBatchTransferIds(batchTransferIds);
    }

    /**
     * 删除微信批量提现账单记录信息
     * 
     * @param batchTransferId 微信批量提现账单记录主键
     * @return 结果
     */
    @Override
    public int deleteLhOrdersWeichatTransferByBatchTransferId(Long batchTransferId)
    {
        return lhOrdersWeichatTransferMapper.deleteLhOrdersWeichatTransferByBatchTransferId(batchTransferId);
    }


    /**
     * 判断用户转账成功还是失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void isTransfer() {
        List<LhOrdersWeichatTransfer> lhOrdersWeichatTransfers = lhOrdersWeichatTransferMapper.selectWeichatTransferByNowList();
        if (lhOrdersWeichatTransfers.size()==0){
            return;
        }
        for (LhOrdersWeichatTransfer lhOrdersWeichatTransfer : lhOrdersWeichatTransfers) {
            TransferBatchService service = wxTiXianUtil.config();
            /** 通过商家批次单号查询批次单 */
            System.err.println("lhOrdersWeichatTransfer.getOutBatchNo() "+lhOrdersWeichatTransfer.getOutBatchNo());
            GetTransferBatchByOutNoRequest request = new GetTransferBatchByOutNoRequest();
            request.setOutBatchNo(lhOrdersWeichatTransfer.getOutBatchNo());
            request.setNeedQueryDetail(true);
            request.setDetailStatus("ALL");
            TransferBatchEntity transferBatchByOutNo = service.getTransferBatchByOutNo(request);
            LhOrdersWeichatTransferRec weichatTransferRec = new LhOrdersWeichatTransferRec();
            // 商家批次单号查询批次单 记录到数据库中
            weichatTransferRec.setTransferRecId(System.currentTimeMillis());
            TransferBatchGet transferBatch = transferBatchByOutNo.getTransferBatch();
            weichatTransferRec.setMchid(transferBatch.getMchid());
            weichatTransferRec.setOutBatchNo(transferBatch.getOutBatchNo());
            weichatTransferRec.setBatchId(transferBatch.getBatchId());
            weichatTransferRec.setAppid(transferBatch.getAppid());
            weichatTransferRec.setBatchStatus(transferBatch.getBatchStatus());
            weichatTransferRec.setBatchType(transferBatch.getBatchType());
            weichatTransferRec.setBatchName(transferBatch.getBatchName());
            weichatTransferRec.setBatchRemark(transferBatch.getBatchRemark());
//            weichatTransferRec.setCloseReason();
            weichatTransferRec.setTotalAmount(transferBatch.getTotalAmount());
            weichatTransferRec.setTotalNum(transferBatch.getTotalNum());
            weichatTransferRec.setCreateTime(transferBatch.getCreateTime());
            weichatTransferRec.setUpdateTime(transferBatch.getUpdateTime());
            weichatTransferRec.setSuccessAmount(transferBatch.getSuccessAmount());
            weichatTransferRec.setSuccessNum(transferBatch.getSuccessNum());
            weichatTransferRec.setFailAmount(transferBatch.getFailAmount());
            weichatTransferRec.setFailNum(transferBatch.getFailNum());
            weichatTransferRec.setTransferSceneId(transferBatch.getTransferSceneId());
//            BeanUtils.copyProperties(weichatTransferRec,transferBatch);
            System.err.println("transferBatch "+transferBatch);
            lhOrdersWeichatTransferRecMapper.insertLhOrdersWeichatTransferRec(weichatTransferRec);


            // 获取批量订单里面的详细内容
            List<TransferDetailCompact> transferDetailList = transferBatchByOutNo.getTransferDetailList();
            if (transferDetailList.size()>0){
                for (TransferDetailCompact transferDetailCompact : transferDetailList) {
                    String outDetailNo = transferDetailCompact.getOutDetailNo();
                    LambdaQueryWrapper<LhMemberWithdrawalRec> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(StringUtils.isNotBlank(outDetailNo),LhMemberWithdrawalRec::getOutDetailNo,outDetailNo);
                    LhMemberWithdrawalRec lhMemberWithdrawalRec = lhMemberWithdrawalRecMapper.selectOne(wrapper);
                    if (lhMemberWithdrawalRec != null){
                        // 将状态添加到微信细节订单记录表里
                        lhMemberWithdrawalRec.setDetailId(transferDetailCompact.getDetailId());
                        lhMemberWithdrawalRec.setDetailStatus(transferDetailCompact.getDetailStatus());
                        lhMemberWithdrawalRecMapper.updateById(lhMemberWithdrawalRec);

                        // 获取订单id
                        LambdaQueryWrapper<LhOrdersWithdrawal> ordersWithdrawal = new LambdaQueryWrapper<>();
                        ordersWithdrawal.eq(LhOrdersWithdrawal::getOrderNumber,lhMemberWithdrawalRec.getOrderNumber());
                        LhOrdersWithdrawal lhOrdersWithdrawal = lhOrdersWithdrawalMapper.selectOne(ordersWithdrawal);

                        //  状态如果成功，就修改主站订单的状态
                        if ("SUCCESS".equals(transferDetailCompact.getDetailStatus())){
                            // 修改
                            LambdaQueryWrapper<LhOrdersWithdrawal> queryWrapper = new LambdaQueryWrapper<>();
                            queryWrapper.eq(LhOrdersWithdrawal::getOrderId, lhOrdersWithdrawal.getOrderId());

                            lhOrdersWithdrawal.setPayed(1);
                            // 日期转换
                            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
                            try {
                                Date parse = df.parse(transferBatch.getCreateTime());
                                lhOrdersWithdrawal.setPayedTime(parse);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            lhOrdersWithdrawal.setUpdateTime(new Date());
                            lhOrdersWithdrawalMapper.updateById(lhOrdersWithdrawal);
                        }
                    }
                }
            }


        }

    }

}
